package org.tuoren.official.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.tuoren.common.idempotent.annotation.RepeatSubmit;
import org.tuoren.common.log.annotation.Log;
import org.tuoren.common.web.core.BaseController;
import org.tuoren.common.mybatis.core.page.PageQuery;
import org.tuoren.common.core.domain.R;
import org.tuoren.common.core.validate.AddGroup;
import org.tuoren.common.core.validate.EditGroup;
import org.tuoren.common.log.enums.BusinessType;
import org.tuoren.common.excel.utils.ExcelUtil;
import org.tuoren.official.domain.vo.HonorsTypeVo;
import org.tuoren.official.domain.bo.HonorsTypeBo;
import org.tuoren.official.service.IHonorsTypeService;
import org.tuoren.common.mybatis.core.page.TableDataInfo;

/**
 * 荣誉资质类别
 *
 * @author wl
 * @date 2024-11-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/official/honorsType")
public class HonorsTypeController extends BaseController {

    private final IHonorsTypeService honorsTypeService;

    /**
     * 前台-查询荣誉资质类别列表
     */
    @SaIgnore
    @GetMapping("/frontList")
    public TableDataInfo<HonorsTypeVo> frontList() {
        return honorsTypeService.frontList();
    }

    /**
     * 查询荣誉资质类别列表
     */
    @SaCheckPermission("official:honorsType:list")
    @GetMapping("/list")
    public TableDataInfo<HonorsTypeVo> list(HonorsTypeBo bo, PageQuery pageQuery) {
        return honorsTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出荣誉资质类别列表
     */
    @SaCheckPermission("official:honorsType:export")
    @Log(title = "荣誉资质类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HonorsTypeBo bo, HttpServletResponse response) {
        List<HonorsTypeVo> list = honorsTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "荣誉资质类别", HonorsTypeVo.class, response);
    }

    /**
     * 获取荣誉资质类别详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("official:honorsType:query")
    @GetMapping("/{id}")
    public R<HonorsTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(honorsTypeService.queryById(id));
    }

    /**
     * 新增荣誉资质类别
     */
    @SaCheckPermission("official:honorsType:add")
    @Log(title = "荣誉资质类别", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HonorsTypeBo bo) {
        return toAjax(honorsTypeService.insertByBo(bo));
    }

    /**
     * 修改荣誉资质类别
     */
    @SaCheckPermission("official:honorsType:edit")
    @Log(title = "荣誉资质类别", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HonorsTypeBo bo) {
        return toAjax(honorsTypeService.updateByBo(bo));
    }

    /**
     * 删除荣誉资质类别
     *
     * @param ids 主键串
     */
    @SaCheckPermission("official:honorsType:remove")
    @Log(title = "荣誉资质类别", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(honorsTypeService.deleteWithValidByIds(List.of(ids), true));
    }
}
